﻿using System.IO;

namespace SVNClean.Modules {
    public class PathNode {
        /// <summary>
        /// Gets or sets the directory.
        /// </summary>
        /// <value>The directory.</value>
        public DirectoryInfo Directory { get; set; }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name {
            get { return Directory != null ? Directory.Name : ""; }
        }

        /// <summary>
        /// Gets the file.
        /// </summary>
        /// <value>The file.</value>
        public FileInfo File {
            get {
                return Directory != null && Directory.IsFile() ?
                    new FileInfo(Directory.FullName) : null;
            }
        }

        /// <summary>
        /// Gets the type.
        /// </summary>
        /// <value>The type.</value>
        public PathContent Type {
            get {
                if (Directory != null) {
                    if (Directory.IsFile()) { return PathContent.File; }
                    else if (Directory.IsDrive()) { return PathContent.Drive; }
                    return PathContent.Directory;
                }
                return PathContent.File;
            }
        }

        private int isSVNDirectory = -1;
        /// <summary>
        /// Gets a value indicating whether this instance is SVN directory.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is SVN directory; otherwise, <c>false</c>.
        /// </value>
        public bool IsSVNDirectory {
            get {
                if (isSVNDirectory == -1 && Directory != null) {
                    isSVNDirectory = Directory.IsSvnDirectory() ? 1 : 0;
                }
                return isSVNDirectory == 1 ? true : false;
            }
        }

        private int isSVNContainer = -1;
        /// <summary>
        /// Gets a value indicating whether this instance is SVN container.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is SVN container; otherwise, <c>false</c>.
        /// </value>
        public bool IsSVNContainer {
            get {
                try {
                    if (isSVNContainer == -1 && Directory != null) {
                        var dirs = Directory.GetDirectories();
                        foreach (var dir in dirs) {
                            if (dir.IsSvnDirectory()) {
                                isSVNContainer = 1;
                                break;
                            }
                        }
                        if (isSVNContainer == -1) isSVNContainer = 0;
                    }
                }
                catch (System.UnauthorizedAccessException) { isSVNContainer = 0; }
                catch (System.IO.IOException) { isSVNContainer = 0; }
                return isSVNContainer == 1 ? true : false;
            }
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString() {
            return Name;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public enum PathContent { File = 0, Directory = 1, Drive = 2 }
}
